package com.kuizii.common.net;

import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageDecoder;

import java.util.List;

/**
 * @author renxiaodong
 */
public class DefaultMessageDecoder extends MessageToMessageDecoder<String> {

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        System.out.println("Client " + ctx.channel().remoteAddress());
        super.channelActive(ctx);
    }


    @Override
    public void decode(ChannelHandlerContext channelHandlerContext, String s, List<Object> list) throws Exception {
        System.out.println("接收消息 " + s);
//        channelHandlerContext.writeAndFlush("ACK");
        channelHandlerContext.fireChannelRead(s);
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        System.out.println("发生解码异常");
        cause.printStackTrace();
        ctx.close();
    }

}
